package com.jd.sys.service.impl;

import com.jd.sys.domain.Role;
import com.jd.sys.mapper.RoleMapper;
import com.jd.sys.service.IRoleService;
import com.jd.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    @Transactional
    public void add(Role role) {
        // 添加维护角色权限表和角色菜单表
        super.add(role);
        if (role.getPermissions()!=null && role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role);
        }
        if (role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.addRoleMenus(role);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        // 删除维护角色权限表和角色菜单表
        roleMapper.delRolePermissionsByRoleId(id);
        roleMapper.delRoleMenusByRoleId(id);
        super.del(id);
    }

    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        // 修改维护角色权限表和角色菜单表
        roleMapper.delRolePermissionsByRoleId(role.getId());
        roleMapper.delRoleMenusByRoleId(role.getId());
        if (role.getPermissions()!=null && role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role);
        }
        if (role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.addRoleMenus(role);
        }
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }

}
